package com.itheima.app.controller;

import com.itheima.app.interceptor.UserHolder;
import com.itheima.app.manager.MessageManager;
import com.itheima.app.manager.UserManager;
import com.itheima.vo.HuanXinVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@RestController
public class

MessageController {

    @Autowired
    private MessageManager messageManager;

    @Autowired
    private UserManager userManager;

    //获取环信用户名和密码
    @GetMapping("/huanxin/user")
    public ResponseEntity findHuanXinUser() {
        //1.创建HuanXinVo对象
        HuanXinVo vo = new HuanXinVo();

        //2.给对象设置用户名和密码
        vo.setUsername("HX" + UserHolder.get().getId());
        vo.setPassword("123456");

        //3.返回
        return ResponseEntity.ok(vo);
    }

    //发送好友申请
    @PostMapping("/tanhua/strangerQuestions")
    public ResponseEntity replyStrangerQuestions(@RequestBody Map<String, String> map) {
        //1.获取推荐用户(对方)的id和回复内容
        Long recommendUserId = Long.parseLong(map.get("userId"));
        String reply = map.get("reply");

        //2.调用manager完成回复信息操作
        return messageManager.replyStrangerQuestions(recommendUserId, reply);
    }

    //添加好友
    @PostMapping("/messages/contacts")
    public void addContact(@RequestBody Map<String, Long> map) {
        Long friendId = map.get("userId");
        messageManager.addContact(friendId);
    }

    // 查看好友列表
    @GetMapping("/messages/contacts")
    public ResponseEntity findContactsByPage(
            @RequestParam(value = "page", defaultValue = "1") Integer pageNum,
            @RequestParam(value = "pagesize", defaultValue = "10") Integer pageSize) {
        return messageManager.findContactsByPage(pageNum, pageSize);
    }

    // 根据环信id查询用户信息
    @GetMapping("/messages/userinfo")
    public ResponseEntity findByUserInfoVo(String huanxinId) {  // HX99
        Long userId = Long.parseLong(huanxinId.replace("HX", ""));
        return userManager.findUserInfoById(userId);
    }

    //点赞列表
    @GetMapping("/messages/likes")
    public ResponseEntity findLikesByPage(
            @RequestParam(value = "page", defaultValue = "1") Integer pageNum,
            @RequestParam(value = "pagesize", defaultValue = "10") Integer pageSize) {
        return messageManager.findCommentsByPage(pageNum, pageSize, 1);
    }

    //评论列表
    @GetMapping("/messages/comments")
    public ResponseEntity findCommentsByPage(
            @RequestParam(value = "page", defaultValue = "1") Integer pageNum,
            @RequestParam(value = "pagesize", defaultValue = "10") Integer pageSize) {
        return messageManager.findCommentsByPage(pageNum, pageSize, 2);
    }

    //喜欢列表
    @GetMapping("/messages/loves")
    public ResponseEntity findLovesByPage(
            @RequestParam(value = "page", defaultValue = "1") Integer pageNum,
            @RequestParam(value = "pagesize", defaultValue = "10") Integer pageSize) {
        return messageManager.findCommentsByPage(pageNum, pageSize, 3);
    }

}